package com.dgty.dg_common.util.voice;

import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.widget.Toast;


import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.util.Locale;

public class AndroidSpeech implements Speech {

    private String TAG = "AndroidSpeech";
    private static AndroidSpeech instance;
    private TextToSpeech textToSpeech; // TTS对象
    private Context context;

    public static synchronized AndroidSpeech getInstance(Context context) {
        if (instance == null) {
            instance = new AndroidSpeech(context);
        }
        return instance;
    }

    public AndroidSpeech(Context context) {
        this.context = context;
        textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int i) {
                LogUtils.dTag(TAG, "init success = " + (1 == TextToSpeech.SUCCESS) + "i=" + i);
                if (i == TextToSpeech.SUCCESS) {
                    textToSpeech.setLanguage(Locale.CHINA);
                    textToSpeech.setPitch(1.0f);// 设置音调，值越大声音越尖（女生），值越小则变成男声,1.0是常规
                    textToSpeech.setSpeechRate(1.0f);
                    int result = textToSpeech.setLanguage(Locale.CHINESE);
                    textToSpeech.setLanguage(Locale.CHINESE);
                } else {

                    if (i == TextToSpeech.LANG_MISSING_DATA || i == TextToSpeech.LANG_NOT_SUPPORTED) {
                        ToastUtils.showShort("LANG_MISSING_DATA  or LANG_NOT_SUPPORTED!");


                    }
                }
            }
        });
    }

    @Override
    public void initApplication(Context context) {

    }

    @Override
    public void startSpeaking(String text, SpeakingListener listener) {
        if (textToSpeech != null) {
            textToSpeech.speak(text,
                    TextToSpeech.QUEUE_ADD, null);
        }
    }

    @Override
    public void setVoicer(int type) {

    }

    @Override
    public String getEngineName() {
        return "ANDROID";
    }

    @Override
    public void pauseSpeaking() {

    }

    @Override
    public void resumeSpeaking() {

    }

    @Override
    public void stopSpeaking() {
        if (textToSpeech != null) {
            textToSpeech.stop();
        }
    }

    @Override
    public void release() {

    }
}